﻿
@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="~/layuimini/lib/layui-v2.6.3/css/layui.css" rel="stylesheet" />
    <link href="~/layuimini/css/public.css" rel="stylesheet" />
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>

    <div id="test4" class="demo-transfer"></div>
    <div>
        <button class="layui-btn layui-btn-normal" id="saveBtn">确认保存</button>
    </div>
    <script src="~/layuimini/lib/layui-v2.6.3/layui.js"></script>
    <script>
        layui.use(['transfer', 'layer', 'util'], function () {
            var $ = layui.$
                , transfer = layui.transfer
                , layer = layui.layer
                , util = layui.util;

            //获取用户备选数据
            $.ajax({
                url: 'http://localhost:31861/api/RoleInfo/GetBindUserInfo',
                type: 'get',
                data: {
                    roleInfoId: parent.roleInfoId
                },
                success: function (res) {
                    var data = [];
                    for (var i = 0; i < res.datas.userInfoOptions.length; i++) {
                        data.push({
                            "value": res.datas.userInfoOptions[i].id, "title": res.datas.userInfoOptions[i].userName
                        });
                    }
                    //显示搜索框
                    transfer.render({
                        elem: '#test4'
                        , data: data
                        , title: ['未绑定', '已绑定']
                        , showSearch: true
                        , id: 'demo1' //定义索引
                        , value: res.datas.userInfoIds
                    })
                }
            })
            //监听提交
            $('#saveBtn').click(function () {
                //判断角色是否存在
                if (parent.roleInfoId) {
                    //获得右侧数据
                    var getData = transfer.getData('demo1');
                    console.log(getData);

                    //获取选择的用户id数组
                    var userInfos = [];
                    for (var i = 0; i < getData.length; i++) {
                        userInfos.push(getData[i].value);
                    }

                    console.log(userInfos)
                    //异步请求添加角色
                    $.ajax({
                        url: 'http://localhost:31861/api/RoleInfo/BinUserInfo',
                        type: 'post',
                        data: {
                            roleInfoId: parent.roleInfoId,
                            userInfoIds: userInfos
                        },
                        success: function (res) {
                            layer.msg(res.msg);
                        }
                    });

                }
                else {
                    layer.msg('你没选择到角色信息');
                }
            });
        });
    </script>
</body>
</html>
